package com.jiangxiaofei.citycloud.activity.fleamarket;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.NeedBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FleaConstant;
import com.jiangxiaofei.citycloud.mvp.FleaPersenter;
import com.jiangxiaofei.citycloud.param.FleaGetDetailParam;
import com.jiangxiaofei.citycloud.util.PermissionUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * @date: 2017/11/16 0016
 * @content: 需求详情页
 */

public class FleaMarketNeedDetailActivity extends BaseActivity implements FleaConstant.GetNeedDetailView {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tv_price)
    TextView tvPrice;
    @InjectView(R.id.tv_look_count)
    TextView tvLookCount;
    @InjectView(R.id.tv_distance)
    TextView tvDistance;
    @InjectView(R.id.tv_date)
    TextView tvDate;
    @InjectView(R.id.tv_description)
    TextView tvDescription;
    @InjectView(R.id.tv_call)
    TextView tvCall;
    FleaGetDetailParam param = new FleaGetDetailParam();
    @InjectView(R.id.tv_qq)
    TextView tvQq;
    private int needId;
    private FleaConstant.GetNeedDetailPersenter getNeedDetailPersenter;
    private String phoneNum;
    private BDLocation bdLocation;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_flea_need_deatail_layout);
        ButterKnife.inject(this);
        mContext = this;
        mActivity = this;
        initTitle();
        Intent intent = getIntent();
        needId = intent.getIntExtra(StringConstants.FLEANEEDID, -1);
        param.setId(needId);
        if (EmptyUtils.isNotEmpty(Constants.bdLocation)) {
            bdLocation = Constants.bdLocation;
        } else {
            Constants.bdclient.start();
            bdLocation = Constants.bdLocation;
        }
        param.setLat(String.valueOf(bdLocation.getLatitude()));
        param.setLng(String.valueOf(bdLocation.getLongitude()));

        getNeedDetailPersenter = new FleaPersenter(mContext, (FleaConstant.GetNeedDetailView) mActivity);
        getNeedDetailPersenter.getNeedDetail();
    }

    @Override
    protected void setListener() {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("需求页面");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public FleaGetDetailParam getNeedDetailParam() {
        return param;
    }

    @Override
    public void needDetailOnSuccess(Object o) {
        List<NeedBean> beans = (List<NeedBean>) o;
        if (beans.size() > 0) {
            NeedBean bean = beans.get(0);
            tvTitle.setText(bean.getTitle());
            tvDate.setText(bean.getIssue());
            tvDistance.setText("距离" + bean.getDistance());
            tvDescription.setText(bean.getDescription());
            tvLookCount.setText(bean.getVisitors());
            tvPrice.setText(bean.getPrice());
            phoneNum = bean.getPhonenum();
        } else {
            Toast.makeText(mContext, "获取数据失败", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.tv_call, R.id.tv_qq})
    public void bOnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_call:
                //通话界面
                initDetailPopup(v1 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.CallPhone(mContext, phoneNum);
                }, v2 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.sendMsg(mContext, phoneNum);
                }, v3 -> {
                    mPopupWindowPublish.dismiss();
                });
                showDetailPopup();
                break;
            case R.id.tv_qq:
                //调起QQ聊天
                String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + "303250762";
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                break;
            default:
                break;
        }
    }

}
